<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="container-fluid">
    <link rel="stylesheet" href="/static/dist/lib/chosen/chosen.css">
    <link rel="stylesheet" href="/static/dist/lib/chosenicons/zui.chosenicons.css">
    资源修改
    <form id="resource-form-add" name="resourceForm" enctype="multipart/form-data" method="post" >
        <input type="hidden" name="id" th:value="${res.id}">
        <input type="hidden" id="oldPid" th:value="${res.pid}">
        <fieldset>
            <div class="form-group">
                <label class="label">资源</label>
                <input type="text" name="name" class="form-control" placeholder="资源名" th:value="${res.name}">
            </div>

            <div class="form-group">
                <label class="label">URL</label>
                <input type="text" name="url" class="form-control" placeholder="资源URL" th:value="${res.url}">
            </div>

            <div class="form-group">
                <label class="label">Icon</label>
                <select class="chosen-icons form-control" name="icon" id="chosenIcons" placeholder="资源Icon" th:attr="data-value=${res.icon}"></select>
            </div>

            <div class="form-group">
                <label class="label">父类资源</label>
                <select class="form-control" name="pid" id="pid">
                    <option value="">无</option>
                </select>
            </div>

            <div class="form-group">
                <label class="label">描述</label>
                <textarea class="form-control" name="description" rows="3" placeholder="描述" th:text="${res.description}"></textarea>
            </div>

            <button type="button" class="btn btn-primary" id="resource_btn_save">保 存</button>
        </fieldset>
    </form>

    <script src="/static/dist/lib/chosen/chosen.js"></script>
    <script src="/static/dist/lib/chosenicons/zui.chosenicons.js"></script>
    <script type="application/javascript">

        /**
         * 初始化上级资源
         */
        $.post('/admin/resource/getResourceParents', {}, function(res) {
            var oldData = $('#oldPid').val();
            var html = "";
            for(index in res.data){
                if(res.data[index]){
                    var optionSelected = (oldData == res.data[index].id) ? ('selected') : ('');
                    html += "<option value='" + res.data[index].id + "'" + optionSelected + ">" + res.data[index].name + "</option>";
                }
            }
            $('#pid').append(html);
            $('#pid').chosen({
                no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
                disable_search_threshold: 10, // 10 个以下的选择项则不显示检索框
                search_contains: true         // 从任意位置开始检索
            });
        });

        /**
         * 初始化ICON Select控件
         */
        $('#chosenIcons').chosenIcons({
            optional: true,
            lang: 'zh_cn',
            icons: {
                common: ['heart', 'user', 'group', 'list-ul', 'th', 'th-large', 'star', 'star-empty', 'search', 'envelope', 'dashboard', 'sitemap', 'umbrella', 'lightbulb', 'envelope-alt', 'cog', 'ok', 'remove', 'home', 'time', 'flag', 'flag-alt', 'flag-checkered', 'qrcode', 'tag', 'tags', 'book', 'bookmark', 'bookmark-empty', 'print', 'camera', 'picture', 'globe', 'map-marker', 'edit', 'edit-sign', 'play', 'stop', 'plus-sign', 'minus-sign', 'remove-sign', 'ok-sign', 'check-sign', 'question-sign', 'info-sign', 'exclamation-sign', 'plus', 'plus-sign', 'minus', 'minus-sign', 'asterisk', 'calendar', 'calendar-empty', 'comment', 'comment-alt', 'comments', 'comments-alt', 'folder-close', 'folder-open', 'folder-close-alt', 'folder-open-alt', 'thumbs-up', 'thumbs-down', 'pushpin', 'building', 'phone', 'rss', 'rss-sign', 'bullhorn', 'bell', 'bell-alt', 'certificate', 'wrench', 'tasks', 'cloud', 'beaker', 'magic', 'smile', 'frown', 'meh', 'code', 'location-arrow'],
                web: ['share', 'pencil', 'trash', 'file-alt', 'file', 'file-text', 'download-alt', 'upload-alt', 'inbox', 'repeat', 'refresh', 'lock', 'check', 'check-empty', 'eye-open', 'eye-close', 'key', 'signin', 'signout', 'external-link', 'external-link-sign', 'link', 'reorder', 'quote-left', 'quote-right', 'spinner', 'reply', 'question', 'info', 'archive', 'collapse', 'collapse-top'],
                editor: ['table', 'copy', 'save', 'list-ol', 'paste', 'keyboard', 'paper-clip', 'crop', 'unlink', 'sort-by-alphabet', 'sort-by-alphabet-alt', 'sort-by-attributes', 'sort-by-attributes-alt', 'sort-by-order', 'sort-by-order-alt'],
                directional: ['chevron-left', 'chevron-right', 'chevron-down', 'chevron-up', 'arrow-left', 'arrow-right', 'arrow-down', 'arrow-up', 'hand-right', 'hand-left', 'hand-up', 'hand-down', 'circle-arrow-left', 'circle-arrow-right', 'circle-arrow-up', 'circle-arrow-down', 'double-angle-left', 'double-angle-right', 'double-angle-down', 'double-angle-up', 'angle-left', 'angle-right', 'angle-down', 'angle-up', 'long-arrow-left', 'long-arrow-right', 'long-arrow-down', 'long-arrow-up', 'caret-left', 'caret-right', 'caret-down', 'caret-up'],
                other: ['desktop', 'laptop', 'tablet', 'mobile', 'building', 'firefox', 'ie', 'opera', 'qq', 'lemon', 'sign-blank', 'circle', 'circle-blank', 'terminal', 'html5', 'android', 'apple', 'windows', 'weibo', 'wechat', 'renren', 'bug', 'moon', 'sun']
            }
        });

        /**
         * 保存按钮
         */
        $('#resource_btn_save').on('click', function () {

            var _this = this;
            var data = $('#resource-form-add').serializeArray();
            var params = {};
            for (index in data) {
                params[data[index].name] = data[index].value;
            }
            $(_this).html('<i class="icon icon-spin icon-spinner-snake"></i>正在保存……')
            $('#resource-form-add fieldset').attr('disabled', true);
            $.post('/admin/resource/update', params, function (res) {
                if(res.code == 200 && res.status){
                    new $.zui.Messager('更新成功。', {
                        type: 'success',
                        icon: 'check',//smile
                        close: true,
                        placement: 'center' // 定义显示位置
                    }).show();
                    setTimeout(function () {
                        // 页面跳转
                        $('body').hrefClick('/admin/resource/index');
                    }, 2000);
                }else{
                    new $.zui.Messager('更新失败，请检查或修改内容后重试。', {
                        type: 'danger',
                        icon: 'frown',//smile
                        close: true,
                        placement: 'center' // 定义显示位置
                    }).show();
                    $(_this).html('保 存');
                    $('#resource-form-add fieldset').attr('disabled', false);
                }
            });
        });

    </script>
</div>
</body>
</html>